package com.mlink.ai.chat.ui.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.mlink.ai.chat.assistant.robot.databinding.ItemExploreCategoryBinding
import com.mlink.ai.chat.bean.ExploreCategory
import com.mlink.ai.chat.bean.ExploreSubCategory
import com.mlink.ai.chat.utils.LinearSpacingItemDecoration
import com.mlink.ai.chat.utils.dp2px

class ExploreAllAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var data = ArrayList<ExploreCategory>()

    private var listener: OnItemOperateListener? = null

    fun setData(data: List<ExploreCategory>) {
        this.data.clear()
        this.data.addAll(data)
    }

    fun getData() = data

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return CategoryViewHolder(
            ItemExploreCategoryBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is CategoryViewHolder) {
            bindCategoryViewHolder(holder, data[position])
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }


    private fun bindCategoryViewHolder(holder: CategoryViewHolder, category: ExploreCategory) {
        holder.binding.title.text = category.categoryName
        holder.binding.seeAll.setOnClickListener {
            listener?.onSeeAll(holder.absoluteAdapterPosition, category)
        }

        holder.binding.recyclerView.apply {
            if (layoutManager == null) {
                layoutManager =
                    LinearLayoutManager(
                        holder.itemView.context,
                        LinearLayoutManager.HORIZONTAL,
                        false
                    )
            }
            if (itemDecorationCount == 0) {
                addItemDecoration(LinearSpacingItemDecoration(7.dp2px(), LinearLayout.HORIZONTAL))
            }

            adapter = ExploreSubCategoryAdapter().apply {
                setOnItemOperateListener(object :
                    ExploreSubCategoryAdapter.OnItemOperateListener {
                    override fun onCreateItemClicked() {
                        listener?.onCreateItemClicked(data[holder.absoluteAdapterPosition])
                    }

                    override fun deleteItem(position: Int, subCategory: ExploreSubCategory) {
                        listener?.deleteItem(position, subCategory)
                    }

                    override fun onItemClicked(subCategory: ExploreSubCategory) {
                        listener?.onSubCategoryItemClicked(
                            data[holder.absoluteAdapterPosition],
                            subCategory
                        )
                    }

                    override fun onDeleteShow(view: View) {
                        listener?.onDeleteShow(view)
                    }

                })
            }

            (adapter as? ExploreSubCategoryAdapter)?.submitList(category.subCategories)
        }
    }

    class CategoryViewHolder(val binding: ItemExploreCategoryBinding) :
        RecyclerView.ViewHolder(binding.root)


    fun setOnItemOperateListener(listener: OnItemOperateListener) {
        this.listener = listener
    }

    interface OnItemOperateListener {

        fun onSeeAll(position: Int, category: ExploreCategory) {

        }

        fun onCreateItemClicked(category: ExploreCategory)

        fun deleteItem(position: Int, subCategory: ExploreSubCategory)

        fun onSubCategoryItemClicked(mainCategory: ExploreCategory, subCategory: ExploreSubCategory)

        fun onDeleteShow(view : View)
    }
}